package activity.signin;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.hutoubenjg.R;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import base.BaseActivity;
import bean.SignInShowBean;
import presenter.LunTanPresenter;
import util.utls.ByAlert;
import util.utls.IsUtilUid;
import util.volley.ResultEntity;
import widget.mydatepicker.DPCManager;
import widget.mydatepicker.DPDecor;
import widget.mydatepicker.DPMode;
import widget.mydatepicker.DatePicker;

/**
 * Created by Administrator on 2017/8/1.
 */
public class SignInShowActivity extends BaseActivity implements DatePicker.OnClickSignIn,Observer {
    ImageView back;
    TextView tvTitle;
    private Context mContext;

    private DPCManager dpcManager;
    DatePicker myDatepicker;

    SignInShowBean signInShowBean;
    TextView jifen,todayDate;
    List<String> tmp = new ArrayList<String>();
    List<String> tmp1= new ArrayList<String>();

    @Override
    protected void onInit() {
        setContentView(R.layout.activity_sign_in_show);

        myDatepicker = (DatePicker) findViewById(R.id.my_datepicker);

        mContext = this;
        tvTitle.setText("签到记录");
        LunTanPresenter lunTanPresenter = new LunTanPresenter(this);
        lunTanPresenter.SignInShow(IsUtilUid.isUid());
        init();





    }

    @Override
    protected void onReadIntent(Bundle bundle) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.back:
                finish();
                break;
        }
    }

    private void init() {

        dpcManager = DPCManager.getInstance();
        dpcManager.clearnDATE_CACHE(); //清除cache

        //自定义背景绘制示例
//        tmp.add("2017-8-1");
//        tmp.add("2017-8-2");
//        tmp1.add("2017-8-3");
//        tmp1.add("2017-8-4");
//


        Log.e("!!!",tmp.toString());
        Log.e("!!!",tmp1.toString());


        dpcManager.setDecorBG(tmp); //预先设置日期背景 一定要在在开始设置

        dpcManager.setDecoUNBG(tmp1); //预先设置日期背景 一定要在在开始设置

        SimpleDateFormat sdfY = new SimpleDateFormat("yyyy");
        SimpleDateFormat sdfM = new SimpleDateFormat("MM");

        myDatepicker.setDate(Integer.parseInt(sdfY.format(System.currentTimeMillis())), Integer.parseInt(sdfM.format(System.currentTimeMillis()))); //设置日期

        myDatepicker.setMode(DPMode.NONE); //设置选择模式

        myDatepicker.setFestivalDisplay(false); //是否显示节日
        myDatepicker.setTodayDisplay(true); //是否高亮显示今天
        myDatepicker.setHolidayDisplay(false); //是否显示假期
        myDatepicker.setDeferredDisplay(false); //是否显示补休
        myDatepicker.setIsScroll(false); //是否允许滑动 false表示左右上下都不能滑动  单项设置上下or左右 你需要自己扩展
        myDatepicker.setIsSelChangeColor(true, getResources().getColor(R.color.font_white_one)); //设置选择的日期字体颜色,不然有的背景颜色和默认的字体颜色不搭

//        myDatepicker.setLeftTitle(2 + "月"); //左上方text
        myDatepicker.setRightTitle(false); //是否签到
        myDatepicker.setOnClickSignIn(this); //点击签到事件

        //设置预先选中日期的背景颜色
        myDatepicker.setDPDecor(new DPDecor() {
            @Override
            public void drawDecorBG(Canvas canvas, Rect rect, Paint paint) {
                Paint paint1 = new Paint();
                paint1.setColor(getResources().getColor(R.color.blue));
                paint1.setStyle(Paint.Style.FILL);
                canvas.drawCircle(rect.centerX(), rect.centerY(), rect.width() / 3F, paint1);
            }
        });
        myDatepicker.setUNDPDecor(new DPDecor() {
            @Override
            public void drawDecorUNBG(Canvas canvas, Rect rect, Paint paint) {
                Paint paint1 = new Paint();
                paint1.setColor(getResources().getColor(R.color.text_gray));
                paint1.setStyle(Paint.Style.STROKE);
                paint1.setStrokeWidth(4);
                canvas.drawCircle(rect.centerX(), rect.centerY(), rect.width() / 3F, paint1);
            }
        });


    }


    @Override
    public void signIn() {

    }

    @Override
    public void update(Observable observable, Object data) {
        ResultEntity entity=handlerError(data);
        if (entity != null) {
            IsUtilUid.tianjia(entity);
            if (entity.getEventType()== LunTanPresenter.signin_success){
              //  ByAlert.alert(entity.getData());
                signInShowBean=new SignInShowBean();
                signInShowBean= (SignInShowBean) entity.getData();
                jifen.setText(signInShowBean.getScore());
                SimpleDateFormat sdfny=new SimpleDateFormat("yyyy.M");
                SimpleDateFormat sdfny1=new SimpleDateFormat("yyyy-M");
                SimpleDateFormat sdfr=new SimpleDateFormat("d");
                todayDate.setText(sdfny.format(System.currentTimeMillis()));
                SimpleDateFormat sdfnyr=new SimpleDateFormat("yyyy-M-d");
                tmp=new ArrayList<>();
                tmp1= new ArrayList<>();
                for(int i=0 ;i< signInShowBean.getSign_info().size();i++){
                    //比当前日期小
                    if( Integer.parseInt(signInShowBean.getSign_info().get(i).getDay())<=Integer.parseInt(sdfr.format(System.currentTimeMillis()))){
                        if("0".equals(signInShowBean.getSign_info().get(i).getTime())){
                            tmp1.add(sdfny1.format(System.currentTimeMillis())+"-"+signInShowBean.getSign_info().get(i).getDay()+"");
                        }else{
                            tmp.add(sdfnyr.format(Long.parseLong(signInShowBean.getSign_info().get(i).getTime())*1000));
                            Log.e("!!!1",signInShowBean.getSign_info().get(i).getTime());
                        }

                    }else {

                    }
                }
                Log.e("!!!1",tmp.toString());
                Log.e("!!!1",tmp1.toString());
                init();
//                dpcManager.setDecorBG(tmp); //预先设置日期背景 一定要在在开始设置
//
//                dpcManager.setDecoUNBG(tmp1); //预先设置日期背景 一定要在在开始设置
//
//
//                myDatepicker.setUNDPDecor(new DPDecor() {
//                    @Override
//                    public void drawDecorUNBG(Canvas canvas, Rect rect, Paint paint) {
//                        Paint paint1 = new Paint();
//                        paint1.setColor(getResources().getColor(R.color.text_gray));
//                        paint1.setStyle(Paint.Style.STROKE);
//                        paint1.setStrokeWidth(4);
//                        canvas.drawCircle(rect.centerX(), rect.centerY(), rect.width() / 3F, paint1);
//                    }
//                });
//
////  //设置预先选中日期的背景颜色
//                myDatepicker.setDPDecor(new DPDecor() {
//                    @Override
//                    public void drawDecorBG(Canvas canvas, Rect rect, Paint paint) {
//                        Paint paint1 = new Paint();
//                        paint1.setColor(getResources().getColor(R.color.blue));
//                        paint1.setStyle(Paint.Style.FILL);
//                        canvas.drawCircle(rect.centerX(), rect.centerY(), rect.width() / 3F, paint1);
//                    }
//                });


            }
            if (entity.getEventType()== LunTanPresenter.signin_fail){
                ByAlert.alert(entity.getData());
            }
        }
        try {
            closeProgressDialog();
        } catch (Exception e) {
        }
    }
}
